(function($){
    $.fn.popupDetails = function(settings) {
        var defaults = {

        };
        settings = $.extend(defaults, settings);
        this.ShowUserDetails = function(userid, company, mapUrl, link, e, officelayoutUrl, confroomUrl) {
            var _userid, _company, _left, _top, _pageY;
            _pageY = e.clientY;
            _left = $(link).offset().left;
            _top = $(link).offset().top;
            _userid = userid;

            gUid = userid;
            gCompany = company;
            gmapUrl = mapUrl;
            gLink = link;
            gEvent = e;
            gOfficeUrl = officelayoutUrl;
            gConferenceUrl = confroomUrl;
            _company = company;
            $("#userName").text("");
            $.ajax({
                type: "GET",
                url: "/user_detail/info/" + _userid,
                data: "comapny=" + _company,
                dataType: "json",
                cache: "true",
                beforeSend: function() {
                    ShowPopupLoaders(link, _pageY, _left, _top, "1");
                },
                complete: function() {
                    HidePopupLoaders("1");
                },
                success: function(response) {
                    if (response != null) {
                        if (response.rows[0] != undefined) {
                            BuildPopupNew(response.rows[0], mapUrl, _left, _top, officelayoutUrl, confroomUrl, _pageY);
                        }
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                //alert("Error:\n" + errorThrown);
                }
            }); // ajax Close
        }
        this.ShowRoomDetails = function(roomId, link, e) {
            var _roomId, _company, _left, _top, _pageY;
            _pageY = e.clientY;
            _left = $(link).offset().left;
            _top = $(link).offset().top;
            _roomId = roomId;

            $.ajax({
                type: "GET",
                url: "/room_detail/info/" + _roomId,
                dataType: "json",
                cache: "true",
                beforeSend: function() {
                    ShowPopupLoaders(link, _pageY, _left, _top,"2");
                },
                complete: function() {
                    HidePopupLoaders("2");
                },
                success: function(response) {
                    if (response != null) {
                        if (response.rows[0] != undefined) {
                            BuildRoomPopup(response.rows[0], _left, _top, _pageY);
                        }
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                //alert("Room_Error:\n" + errorThrown);
                }
            });  // ajax Close
        }
        return this.each(function(index) {

            //
            });
        function ShowPopupLoaders(nameLink, pageY, left, top, flag) {
            SetPopupOffset(pageY, left, top, flag);
            if (flag == "1") {
                $("#divContent").hide();
                $("#divUserDetails").show();
                $(".loaderimg").show();
            }
            else if (flag == "2") {
                $("#meetingroom_details").hide();
                $("#meetingroom_cnt").show();
                $(".meetingroom_loaderimg").show();
            }
        }
        function HidePopupLoaders(flag) {
            if (flag == "1") {
                $(".loaderimg").hide();
            }
            else if (flag == "2") {
                //$(".meetingroom_loaderimg").show();
                $(".meetingroom_loaderimg").hide();
            }
        }
        function SetPopupOffset(pageY, left,top, flag) {
            var _left = left;
            var _top = top;
            var _contentHeight;
            if (flag == "1") {
                $("#divUserDetails").css("left", (_left + 90) + "px");
                _contentHeight = $("#divContent").height();
                var screenHeight = parseInt($(window).height());
                if (_contentHeight > 0) {
                    $("#divUserDetails").css("height", (_contentHeight + 75) + "px");
                }
                var popupHeight = parseInt($("#divUserDetails").css("height"));
                if ((screenHeight - pageY) >= popupHeight) {
                    $("#divUserDetails").css("top", (_top - 10) + "px");
                    $("div.left_arrow img").attr("src", "images/arrow.jpg");
                    $("div.left_arrow").css("top", "0px");
                }
                else {
                    $("#divUserDetails").css("top", (_top + 38 - popupHeight) + "px");
                    $("div.left_arrow img").attr("src", "images/arrow_lt.jpg");
                    $("div.left_arrow").css("top", (popupHeight - 53) + "px");
                }
            }
            else if (flag == "2") {
                $("#meetingroom_cnt").css("left", (_left + 120) + "px");
                var screenHeight = parseInt($(window).height());
                $("#meetingroom_cnt").css("height", $("#meetingroom_details").css("height"))
                var popupHeight = parseInt($("#meetingroom_cnt").css("height"));
                if ((screenHeight - pageY) >= popupHeight) {
                    $("#meetingroom_cnt").css("top", (_top - 10) + "px");
                    $("div.meetingroom_left_arrow img").attr("src", "images/arrow.jpg");
                    $("div.meetingroom_left_arrow").css("top", "0px");
                }
                else {
                    $("#meetingroom_cnt").css("top", (_top - popupHeight - 32) + "px");
                    $("#meetingroom_cnt").css("height", $("#meetingroom_details").css("height"))
                    $("div.meetingroom_left_arrow img").attr("src", "images/arrow_lt.jpg");
                    $("div.meetingroom_left_arrow").css("top", "260px");
                }
            }
        }
        function BuildPopupNew(userDataset, mapUrl, left, top, officelayoutUrl, confroomUrl,pageY) {
            var imgUrl, empNumber, name, title, company, department, email, phoneNo, mobile, address, manager, skypeId, wikiUrl, _left, _top, skypeChat, cubeLocation;
            _left = left;
            _top = top;

            name = userDataset.givenname != null ? userDataset.givenname : "";
            title = userDataset.jobtitle != null ? userDataset.jobtitle : "";
            company = userDataset.company != null ? userDataset.company : "";
            department = userDataset.department != null ? userDataset.department : "";
            email = userDataset.mail != null ? userDataset.mail : "";
            phoneNo = userDataset.phonework != null ? userDataset.phonework : "";
            mobile = userDataset.mobile != null ? userDataset.mobile : "";
            address = userDataset.streetaddress != null ? userDataset.streetaddress : "";
            manager = userDataset.manager != null ? userDataset.manager : "";
            skypeId = userDataset.skypename != null ? userDataset.skypename : "";
            wikiUrl = userDataset.wikiurl != null ? userDataset.wikiurl : "";
            empNumber = userDataset.samaccountName != null ? userDataset.samaccountName : "";
            cubeLocation = userDataset.cubelocation != null ? userDataset.cubelocation : "";
            $("#HiddenSkype").val(skypeId);
            if (skypeId != "") {
                skypeChat = '<a href="skype:' + skypeId + '?chat">Start Skype Chat</a>';
                skypeId =skypeId+ '&nbsp;<a href="skype:' + skypeId + '?add"><img src="images/icon_features_add.gif" border="0" /></a>';
            }else{
                skypeChat="";
            }
            $("#empImage").attr('src','');
            $("#tdName").text(name);
            $("#tdTitle").text(title);
            $("#tdCompany").text(company);
            $("#tdDepartment").text(department);
            $("#tdEmail").html('<a href="mailto:' + email + '">' + email + '</a>');
            $("#tdPhone").html(phoneNo + '&nbsp;<a id="pModify" href="edituser/' + empNumber + '" target="_top" style="cursor:pointer"><img src="images/pencil.png" border="0px"/></a>');
            $("#tdMobile").html(mobile + '&nbsp;<a id="cModify" href="edituser/' + empNumber + '" target="_top" style="cursor:pointer"><img src="images/pencil.png" border="0px"/></a>');
            $("#tdAddress").text(address);
            $("#tdCubeLocation").html(cubeLocation+'&nbsp;<a id="locModify" href="edituser/' + empNumber + '" target="_top" style="cursor:pointer"><img src="images/pencil.png" border="0px"/></a>');
            $("#tdManager").text(manager);
            $("#tdSkype").html(skypeId + '&nbsp;<a id="aModify" href="edituser/' + empNumber + '" target="_top" style="cursor:pointer"><img src="images/pencil.png" border="0px"/></a>');
            $("#tdSkypeChat").html(skypeChat );
            $("#userName").text("Details for " + name);
            $("#aVcf").attr("href", "export/vcf/" + empNumber);
            $("#HiddenEmpNumber").val(empNumber);

            //uploadImage(GetImageURL(name));//displaying photo of employee

            if (wikiUrl != "" && wikiUrl !="undefined") {
                $("#tdWikiPage").html('<a href="' + wikiUrl + '" target="blank">' + wikiUrl + '</a>&nbsp;<a id="wModify" href="edituser/' + empNumber + '" target="_top" style="cursor:pointer"><img src="images/pencil.png" border="0px"/></a>');
            }
            else {
                $("#tdWikiPage").html('&nbsp;<a id="wModify" href="edituser/' + empNumber + '" target="_top" style="cursor:pointer"><img src="images/pencil.png" border="0px"/></a>');
            }
            console.log(mapUrl);

            if(mapUrl.length > 1)
                $("#gmapFrame").attr("src", mapUrl);

            if (mapUrl != "") {
                $("#person_photo").css("margin", "-22px 10px 10px");
            }
            else {
                $("#person_photo").css("margin", "-16px 10px 10px");
            }
            if (officelayoutUrl != "") {
                $("#lnkLocation").attr("href", officelayoutUrl + "#search='" + name + "'&zoom=150");
                $("#lnkLocation").show();
                $("#liSeperator").show();
            }
            else {
                $("#lnkLocation").hide();
                $("#liSeperator").hide();
            }
            if (confroomUrl != "") {
                $("#lnkRoom").attr("href", "javascript:ShowConferenceRoom('" + confroomUrl + "')");
                $("#lnkRoom").show();
            }
            else {
                $("#lnkRoom").hide();
            }
            //If company is not present against user then hide View Location row
            if (confroomUrl == "" && officelayoutUrl == "")
                $("#trLocation").hide();

            //Set the offset attributes of divUserDetails based on the clicked link
            SetPopupOffset(pageY, _left, _top, "1");
            $("#divUserDetails").fadeIn("slow");
            $("#divContent").show();

            $('#aModify, #pModify, #pModify, #cModify, #locModify, #wModify').click(function() {
                $("#divUserDetails").hide();
            }).popupWindow({
                height: 280,
                width: 550,
                centerBrowser: 1
            });
        }

        function uploadImage(imageUrl) {
            var imgUrl=imageUrl+""+'.JPG';
            $.ajax({
                url: imgUrl,
                type: 'HEAD',
                success: function() {
                    $("#empImage").attr('src',imgUrl);
                },
                error: function() {
                    PNGImage(imageUrl);
                }
            });
        }

        function PNGImage(imageURL) {
            var imgUrl=imageURL+""+'.PNG';
            $.ajax({
                url: imgUrl,
                type: 'HEAD',
                success: function() {
                    $("#empImage").attr('src',imgUrl);
                },
                error: function() {
                    GIFImage(imageURL);
                }
            });
        }
        function GIFImage(imageURL) {
            var imgUrl=imageURL+""+'.GIF';
            $.ajax({
                url: imgUrl,
                type: 'HEAD',
                success: function() {
                    $("#empImage").attr('src',imgUrl);
                },
                error: function() {
                    $("#empImage").attr('src','images/ico_person.gif');
                }
            });
        }

        function GetImageURL(name) {
            var names=name.split(" ");
            var imageName=names[0]+"_"+names[names.length-1];
            var imgUrl='employee_images/'+imageName;
            return imgUrl;
        }

        function BuildRoomPopup(roomDataset, rLeft, rTop, rPageY) {
            var rName, rOffice, rPhone, rBridge, rPin, rSeats, rResources, rOfficeLocation, rInternalDial;
            rName = roomDataset.RoomName != null ? roomDataset.RoomName : "";
            rOffice = roomDataset.OfficeName != null ? roomDataset.OfficeName : "";
            rPhone = roomDataset.PhoneNumber != null ? roomDataset.PhoneNumber : "";
            rOfficeLocation = roomDataset.OfficeLocation != null ? roomDataset.OfficeLocation : "";
            rBridge = roomDataset.ConferenceBridge != null ? roomDataset.ConferenceBridge : "";
            rPin = roomDataset.Pin != null ? roomDataset.Pin : "";
            rSeats = roomDataset.Seats != null ? roomDataset.Seats : "";
            rResources = roomDataset.Resources != null ? roomDataset.Resources : "";
            rInternalDial = roomDataset.InternalDial != null ? roomDataset.InternalDial : "";
            $(".meetingroom_title_font").text(rName);
            $("#tdRoomOffice").text(rOffice);
            $("#tdRoomPhone").text(rPhone);
            $("#tdRoomOfficeLoc").text(rOfficeLocation);
            $("#tdRoomConfBridge").text(rBridge);
            $("#tdRoomPin").text(rPin);
            $("#tdRoomSeats").text(rSeats);
            if (rResources == "")
                $($("#tdRoomResources").parent()).hide();
            else {
                $("#tdRoomResources").text(rResources);
                $($("#tdRoomResources").parent()).show();
            }
            if (rInternalDial == "")
                $($("#tdRoomIntDial").parent()).hide();
            else {
                $("#tdRoomIntDial").text(rInternalDial);
                $($("#tdRoomIntDial").parent()).show();
            }

            //Set the offset attributes of divUserDetails based on the clicked link
            SetPopupOffset(rPageY, rLeft, rTop, "2");
            $("#meetingroom_details").show();
            $("#meetingroom_cnt").fadeIn("slow");
        }
        
    };
})(jQuery);

